If you are a WordPress developer and love to create custom post types to addon the new feature in your project then this article is very helpful for you.
When you create a new custom post type in WordPress then you can see that by default, its slug will show in the permalink URL structure. Adding slug to permalink displays the post type of the current post and creates the URLs longer which is hard to remember. Also, it creates an issue to rank articles in Google.
So now I will cover all steps to remove slug from custom post type to make URL shorter and easy to remember.
Custom Post Type in WordPress
Recently, I created a custom post type called “Online Services” for a company. This resulted in permalinks such as example.com/online_services/title-of-online-service
As you can see, having permalinks like example.com/online_services/title-of-online-service
is not nearly as clean, also not easy to remember and search engine optimized.
But having permalinks like
is nearly as clean, easy to remember, and SEO friendly.example.com/title-of-online-service
As SEO (Search Engine Optimization) experts always suggest making URL shorter highly rank in Google.
I hope now you are clear with the article’s focused point. Now let’s see the solution:
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Remove slug from custom post type
Step-1: Create your custom post type if you are not already created it till now.
Step-2: Copy and Paste the following code to your theme functions.php file to filter the permalink for our custom post type so that all published posts don’t have the slug in their URLs:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php /** * Remove the slug from published post permalinks. Only affect our custom post type, though. */ function wptw_remove_cpt_slug( $post_link, $post ) { if ( 'online_services' === $post->post_type && 'publish' === $post->post_status ) { $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); } return $post_link; } add_filter( 'post_type_link', 'wptw_remove_cpt_slug', 10, 2 ); |
The above code will work, but conflicts may happen if the slug for your custom post type is the same as a page or post’s slug.
So removing the slug isn’t enough. You’ll get a 404 page if the slug for your custom post type is the same as a page or post’s slug because WordPress only expects posts and pages to behave this way.
Step-3: Now you also need to add the following: to your theme functions.php file to teach it that our custom post type’s posts can also have URLs like pages or posts.
2 3 4 5 6 7 8 9 10 11 12 13 14 |
function wptw_parse_request( $query ) { if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'online_services', 'page' ) ); } } add_action( 'pre_get_posts', 'wptw_parse_request' ); |
If the above code will not work with your theme then remove the above code and try to use the below code:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// instruct wordpress on how to find posts from the new permalinks function parse_request_remove_cpt_slug( $query_vars ) { // return if admin dashboard if ( is_admin() ) { return $query_vars; } // return if pretty permalink isn't enabled if ( ! get_option( 'permalink_structure' ) ) { return $query_vars; } $cpt = 'online_services'; // store post slug value to a variable if ( isset( $query_vars['pagename'] ) ) { $slug = $query_vars['pagename']; } elseif ( isset( $query_vars['name'] ) ) { $slug = $query_vars['name']; } else { global $wp; $path = $wp->request; // use url path as slug if ( $path && strpos( $path, '/' ) === false ) { $slug = $path; } else { $slug = false; } } if ( $slug ) { $post_match = get_page_by_path( $slug, 'OBJECT', $cpt ); if ( ! is_admin() && $post_match ) { // remove any 404 not found error element from the query_vars array because a post match already exists in cpt if ( isset( $query_vars['error'] ) && $query_vars['error'] == 404 ) { unset( $query_vars['error'] ); } // remove unnecessary elements from the original query_vars array unset( $query_vars['pagename'] ); // add necessary elements in the the query_vars array $query_vars['post_type'] = $cpt; $query_vars['name'] = $slug; $query_vars[$cpt] = $slug; // this constructs the "cpt=>post_slug" element } } return $query_vars; } add_filter( 'request', "parse_request_remove_cpt_slug" , 1, 1 ); |
That’s it! Just replace “online_services” with the slug of your custom post type in both code samples. After this Go to Settings
> Permalinks
and saving the permalink structure to end in /%postname%/
may also be necessary.
Wrapping Words!
Thanks for reading 🙏, I hope you found the Remove slug from custom post type in WordPress Without Plugin tutorial helpful for your project. Keep learning!. If you face any problem – I am here to solve your problems.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co